package com.wwy.arithmetic.suanfa;

import java.util.ArrayList;
import java.util.List;

/**
 * 判断一个数字是否是回文数字
 *
 * @author 20706
 */
public class HuiWenZhengShu {

    public static void main(String[] args) {
        boolean judge = judge(10);
        System.out.println("judge = " + judge);
    }

    static boolean judge(int x) {
        //如果小于0该数一定不可能是回文数字 151  1001
        if (x < 0) {
            return false;
        }
        List<Integer> list = new ArrayList<>();
        while (x >= 10) {
            list.add(x % 10);
            x = x / 10;
        }
        list.add(x % 10);
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (!list.get(i).equals(list.get(list.size() - 1 - i))) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 第二种解法
     */
    static boolean judge1(int x) {
        if (x < 0 || (x % 10 == 0 && x != 0)) {
            return false;
        }
        int reverseNumber = 0;
        while (x > reverseNumber) {
            reverseNumber = reverseNumber * 10 + x % 10;
            x /= 10;
        }
        return x == reverseNumber || x == reverseNumber / 10;
    }
}
